<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: db_operations.php 43 2013-09-21 11:56:06Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 43 $
* 	zuletzt geaendert von:	$Author: peter $
* 	Datum:					$Date: 2013-09-21 11:56:06 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

switch($_GET['action']) {
	case 'sql_upload':
		if(isset($_FILES['sql_update'])) {
			require(DIR_FS_INC.'inc.sql_query.php');
			$target = DIR_FS_CATALOG.'cache/'.$_FILES['sql_update']['name'];
			move_uploaded_file($_FILES['sql_update']['tmp_name'], $target);
			$warnings = sql_query($target, $_SESSION['languages_id']);
			unlink($target);
			
			if(sizeof($warnings) > 0){
				for($i = 0; $i < sizeof($warnings); $i++)
					$message_stack->add_session($warnings[$i]->fields['Message'], 'error' ,true);
			}else
				$message_stack->add_session(SQL_UPDATE_SUCCESS, 'success');
				
			redirect(href_link(FILENAME_DB_OPERATIONS));
		}
		break;
	
	case 'kill':
			$db->db_query("KILL '".(int)$_GET['kill']."'");
			$message_stack->add_session('Prozess '.$_GET['kill'].' wurde erolgreich beendet.', 'success');
			redirect(href_link('db_operations.php', 'reopen=true'));
		break;
	
	case 'repair':
		if(isset($_POST['repaire_table']) && !empty($_POST['repaire_table'])){
			if(sizeof($_POST['table']) > 0) {
				for($i=0;$i<=sizeof($_POST['table']);$i++) {
					if(isset($_POST['table'][$i]) && !empty($_POST['table'][$i])){
						$msg = $db->db_query($_POST['repaire_table']." TABLE ".$_POST['table'][$i]);
						$db->db_query($_POST['repaire_table']." TABLE ".$_POST['table'][$i]);
						$db->db_query("SHOW WARNINGS");
						if($msg->fields['Msg_type'] == 'error' || $msg->fields['Msg_type'] == 'warning')
							$message_stack->add_session($msg->fields['Table'].' meldet: '.$msg->fields['Msg_text'], $msg->fields['Msg_type'], true);
						elseif (($msg->fields['Msg_type'] == 'status' || $msg->fields['Msg_type'] == 'note') && ($_POST['repaire_table'] == 'ANALYZE' || $_POST['repaire_table'] == 'CHECK'))
							$message_stack->add_session($msg->fields['Table'].' meldet: '.$msg->fields['Msg_text'], $msg->fields['Msg_type'], false);
					}
				}
				redirect(href_link('db_operations.php'));
			} else {
				$message_stack->add_session('Wählen Sie mindestens eine Tabelle aus!', 'warning');
				redirect(href_link('db_operations.php'));
			}
		}
		if(isset($_POST['save_table']) && !empty($_POST['save_table'])) {
			include(DIR_WS_MODULES.'db_operations/backup.php');
			$message_stack->add_session('Befehl wurde ausgeführt. Eine Kopie dieser Datei liegt auch auf dem Server im Export-Verzeichnis', 'success');
			redirect(href_link('db_operations.php'));
		}
		break;
}

switch($_GET['page']) {
	case 'upload':
		include(DIR_WS_MODULES.'db_operations/upload.php');
		break;

	case 'process':
		include(DIR_WS_MODULES.'db_operations/process.php');
		echo '<script type="text/javascript">$(function() {setTimeout(function(){$(\'#list\').load(\'?page=process\');}, 5000);});</script>';
		break;

	default:
		require(DIR_WS_INCLUDES.'metatag.php');
		?>
		</head>
		<body>
			<?php require(DIR_WS_INCLUDES.'header.php'); ?>
			<div id="wrapper">
				<table class="outerTable" cellspacing="0" cellpadding="0">
					<tr>
						<td class="boxCenter" height="100%" valign="top">
							<table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
								<tr>
									<td class="pageHeading"><?php echo HEADING_TITLE.$table_status->_numOfRows.HEADING_TITLE_ADD; ?></td>
								</tr>
							</table>
							<div class="ar">
								<?php
								$table_status = $db->db_query("SHOW TABLE STATUS FROM ".DB_DATABASE);
		
								$operations = array(array('id' => '','text' => TEXT_NONE),
													array('id' => 'ANALYZE','text' => TABLE_DATABASE_OPERATION_ANALYZE),
													array('id' => 'CHECK','text' => TABLE_DATABASE_OPERATION_CHECK),
													array('id' => 'OPTIMIZE','text' => TABLE_DATABASE_OPERATION_OPTIMIZE),
													array('id' => 'REPAIR', 'text' => TABLE_DATABASE_OPERATION_REPAIR));

								$compress = array(	array('id' => 'sql','text' => 'SQL'),
													array('id' => 'zip','text' => 'ZIP'),
													array('id' => 'gzip','text' => 'GZIP'));

								$action = array(array('id' => '','text' => TEXT_NONE),
											array('id' => 'complete','text' => 'Komplettsicherung'),
											array('id' => 'only_fields','text' => 'ausgewählte Tabellen Strukturen'),
											array('id' => 'only_data','text' => 'ausgewählte Tabellen Daten'),
											array('id' => 'fields_data','text' => 'ausgewählte Tabellen Strukturen + Daten'));
								echo get_help('30', 'laufende Datenbankprozesse') ?>
								<button class="button" onclick="dialogBox('?page=process', '750', '500', 'laufende Datenbankprozesse')">
									Datenbankprozesse
								</button> 
								<button class="button" onclick="dialogBox('?page=upload', '400', '140', '<?php echo SQL_UPDATE_TEXT ?>', 'true')">
									Upload SQL
								</button>
							</div><br />
							<?php echo draw_form('operation', FILENAME_DB_OPERATIONS, 'action=repair'); ?>
								<table width="100%" cellpadding="4" class="dataTable">
									<tr class="dataTableHeadingRow">
										<th class="dataTableHeadingContent"><input type="checkbox" onclick="check_box(this.checked,'table')" /></th>
										<th class="dataTableHeadingContent"><?php echo TABLE_HEADING_DATABASE_OPERATION_NAME; ?></th>
										<th class="dataTableHeadingContent"><?php echo TABLE_HEADING_DATABASE_OPERATION_ENGINE; ?></th>
										<th class="dataTableHeadingContent"><?php echo TABLE_HEADING_DATABASE_OPERATION_ROWS; ?></th>
										<th class="dataTableHeadingContent"><?php echo TABLE_HEADING_DATABASE_OPERATION_DATA; ?></th>
										<th class="dataTableHeadingContent"><?php echo TABLE_HEADING_DATABASE_OPERATION_INDEX; ?></th>
										<th class="dataTableHeadingContent"><?php echo TABLE_HEADING_DATABASE_OPERATION_OVERHANG; ?></th>
									</tr>						
									<?php
									while(!$table_status->EOF){ 
										if ($rows % 2 == 0) $f = 'dataTableRow';
										else $f = '';
									
									echo '<tr class="'.$f.'" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'pointer\'" onmouseout="this.className=\''.$f.'\'" class="dataTableRow">';?>		
										<td class="dataTableContent ac">
											<input type="checkbox" id="checkbox_table_<?php echo $rows; ?>" name="table[]" value="<?php echo $table_status->fields['Name']; ?>" />
										</td>
										<td class="dataTableContent al">
											<label style="-moz-user-select: none;padding-right:100%; padding-top: 5px; padding-bottom: 5px;" for="checkbox_table_<?php echo $rows; ?>"> <?php echo $table_status->fields['Name']; ?></label>
										</td>
										<td class="dataTableContent ar"><?php echo $table_status->fields['Engine']; ?></td>
										<td class="dataTableContent ar"><?php $t_rows +=$table_status->fields['Rows']; echo $table_status->fields['Rows']; ?></td>
										<td class="dataTableContent ar"><?php $t_dlength +=$table_status->fields['Data_length']; echo check_size($table_status->fields['Data_length']); ?></td>
										<td class="dataTableContent ar"><?php $t_ilength += $table_status->fields['Index_length']; echo check_size($table_status->fields['Index_length']); ?></td>
										<td class="dataTableContent ar"><?php $t_free +=$table_status->fields['Data_free']; echo check_size($table_status->fields['Data_free'],'2' ,'check'); ?></td>
									</tr>
									<?php
										$rows++;
										$table_status->MoveNext();
									}
									?>
									<tr>
										<td class="ar" colspan="3"><b>Gesamt:</b></td>
										<td class="ar"><b><?php echo $t_rows ?></b></td>
										<td class="ar"><b><?php echo check_size($t_dlength) ?></b></td>
										<td class="ar"><b><?php echo check_size($t_ilength) ?></b></td>
										<td class="ar"><b><?php echo check_size($t_free,'2' ,'check') ?></b></td>
									</tr>
									<tr>
										<td class="dataTableContent al" colspan="3">
											<b><?php echo get_help('29', 'Datenbank BackUp') ?> Backup:</b> <?php echo draw_pull_down_menu('compress', $compress, '', 'style="width:auto"').' '.draw_pull_down_menu('save_table', $action, '', 'onchange="this.form.submit()"');?>
										</td>
										<td class="dataTableContent ar" colspan="4">
											<b>Operationen:</b> <?php echo draw_pull_down_menu('repaire_table', $operations, '', 'onchange="this.form.submit()"');?>
										</td>	
									</tr>
								</table>
							</form>
						</td>
					</tr>
				</table>
			</div>
			<script type="text/javascript" src="includes/javascript/jquery.fixedTableHeader.js"></script>
			<script type="text/javascript">
				$(function() {
					$('.dataTable').fixedtableheader();
				});   
				function check_box(check,praefix) {
					var fields = document.forms["operation"].elements;
					for(i=0;i<fields.length;i++) {
						var field = fields[i];
						if((field.name.substr(0,praefix.length) == praefix) && (field.type == 'checkbox')) {
							field.checked = check;
						}
					}
				}
				function dialogBox(url, w, h, title, save) {
					<?php if(isset($_SESSION['customer_id'])) { ?>
						if(save == 'true') {
							var extra_button = {"<?php echo BUTTON_SAVE ?>":function(){$("#dialog_form").submit();},"<?php echo BUTTON_CANCEL ?>":function(){$(this).dialog("close");}};
						} else {var extra_button = '';}
						
						var url = url;
			            var dialog = $('<div style="display:none" class="loading"></div>').appendTo('body');
			            
			            dialog.dialog({
			                close: function(event, ui) {dialog.remove();},
			                modal: true,
			                title: title,
			                buttons: extra_button,
			                width: w,
			                height: h
			            });
			            dialog.load(url,{},function (responseText, textStatus, XMLHttpRequest){
			            	dialog.removeClass('loading');
			            	$('.ui-dialog-content').css({backgroundImage:'none'});
			            });
			            $('.ui-dialog').css({position:"fixed"});
					<?php } else { ?>
						window.location.href='<?php echo catalog_href_link('login.php', '', 'SSL'); ?>';
					<?php } ?>
					return false;
				}
				<?php
				if(isset($_GET['reopen']))
					echo 'dialogBox(\'?page=process\', 750, 500, \'laufende Datenbankprozesse\');';
				?>
			</script>
			<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
		</body>
		</html>
		<?php require(DIR_WS_INCLUDES.'application_bottom.php');
		break;
}